// https: blog.csdn.net/ABC123220/article/details/126035746
type logType = string | number | object

// 操作日志类
class Logs {
  // 当前索引
  private activeIndex: number
  // 日志列表
  private logList: logType[]

  // 构造函数
  constructor(activeIndex: number = 0, logList = []) {
    this.activeIndex = activeIndex
    this.logList = logList
  }

  /**
   * 添加日志
   * @param val
   */
  add(val: logType) {
    if (this.logList.at(-1) !== val) {
      let obj: any = {}
      if (typeof val === 'object') {
        obj = val
        obj.createTime = new Date().getTime()
      } else {
        obj.key = val
        obj.createTime = new Date().getTime()
      }
      this.logList.push(obj)
      this.activeIndex = this.logList.length - 1
    }
  }

  /**
   * 日志列表是否为空
   * @returns
   */
  isLogNull(): boolean {
    return this.logList.length === 0
  }

  /**
   * 上一个
   * @returns
   */
  prev(): logType {
    if (!this.isPositionStart()) {
      this.activeIndex--
      return this.logList[this.activeIndex]
    }

    return this.logList.length ? this.logList[0] : ''
  }

  /**
   * 下一个
   * @returns
   */
  next(): logType {
    if (!this.isPositionEnd()) {
      this.activeIndex++
      return this.logList[this.activeIndex]
    }
    return this.logList.length ? this.logList.at(-1)! : ''
  }

  /**
   * 是否在开始位置
   * @returns
   */
  isPositionStart(): boolean {
    return this.activeIndex === 0
  }

  /**
   * 是否在结束位置
   * @returns
   */
  isPositionEnd(): boolean {
    return this.activeIndex === this.logList.length - 1
  }
}

export default Logs
